package com.hqyj.ssm_project.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component("dispatcherServletException")
public class     DispatcherServletException implements HandlerExceptionResolver {
    private Logger logger = LoggerFactory.getLogger(DispatcherServletException.class);
    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView modelAndView=new ModelAndView();
        if(ex instanceof ServiceException){
            modelAndView.addObject("exceptioninfo",ex.getMessage());
            ex.printStackTrace();
            logger.debug(ex.getLocalizedMessage());
        }else if(ex instanceof ControllerException){
            modelAndView.addObject("exceptioninfo",ex.getMessage());
            ex.printStackTrace();
            logger.debug(ex.getLocalizedMessage());
        }else{
            modelAndView.addObject("exceptioninfo","发生其他错误");
            ex.printStackTrace();
            logger.debug(ex.getLocalizedMessage());
        }
        modelAndView.setViewName("exception");
        return modelAndView;
    }
}
